<template>
  <div :class="{'hidden':hidden}" class="pagination-container">
    <el-pagination
      :background="background"
      :current-page.sync="currentPage"
      :page-size.sync="pageSize"
      :layout="layout"
      :page-sizes="pageSizes"
      :total="total"
      v-bind="$attrs"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <div class="rightPopover">
<!--  popover 自定义展示列-->
      <slot name="popover"></slot>
    </div>
  </div>
</template>

<script>
import { scrollTo } from '@/utils/scroll-to'

export default {
  name: 'Pagination',
  props: {
    total: {
      required: true,
      type: Number
    },
    page: {
      type: Number,
      default: 1
    },
    limit: {
      type: Number,
      default: 20
    },
    pageSizes: {
      type: Array,
      default() {
        return [10, 50, 500]
      }
    },
    layout: {
      type: String,
      default: 'total, sizes, prev, pager, next, jumper'
    },
    background: {
      type: Boolean,
      default: true
    },
    autoScroll: {
      type: Boolean,
      default: true
    },
    hidden: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    currentPage: {
      get() {
        return this.page
      },
      set(val) {
        this.$emit('update:page', val)
      }
    },
    pageSize: {
      get() {
        return this.limit
      },
      set(val) {
        this.$emit('update:limit', val)
      }
    }
  },
  methods: {
    handleSizeChange(val) {
      this.$emit('pagination', { page: this.currentPage, limit: val })
      if (this.autoScroll) {
        scrollTo(0, 800)
      }
    },
    handleCurrentChange(val) {
      this.$emit('pagination', { page: val, limit: this.pageSize })
      if (this.autoScroll) {
        scrollTo(0, 800)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.pagination-container {
  height: 40px;
  background: #fff;
  margin: 0;
  padding: 0 !important;
  .el-pagination{
    left: 0;
    padding: 4px 10px;
    border: 1px solid #dfe6ec;
    border-top: none;
  }
  /*自定义展示列*/
  .rightPopover{
    position: absolute;
    right: 10px;
    ::v-deep .el-checkbox{
      display:block;
      height:20px;
      line-height:20px;
      padding:0 5px;
      margin-right:0;
      font-size:12px;
      border: 1px solid transparent;
    }
    ::v-deep .selectMenu{
      max-height: 500px;
      overflow: auto;
    }
    ::v-deep .selectBtn{
      margin-top: 4px;
      padding: 3px 2px;
      border-color: #EEF0F3;
      i:first-child{
        font-size: 20px;
        color: #adb2bd;
      }
      i:last-child{
        font-size: 16px;
        color: #eff0f3;
      }
      &:hover{
        background: #fff;
        i:first-child{
          color: #848ae3;
        }
        i:last-child{
          color: rgba(132, 138, 227, .5)
        }
      }
    }
  }
}
.pagination-container.hidden {
  display: none;
}
</style>
